home *** CD-ROM | disk | FTP | other *** search
- #include <stdlib.h>
- #include <stdio.h>
- #include <conio.h>
- #include <graphics.h>
- #include <io.h>
- #include <malloc.h>
- #include <string.h>
- #include <math.h>
-
-
-
- void main(int argc, char **argv)
- {
- int i,j,k,l;
- FILE *datafile;
- unsigned char LUT[256][3];
- float temp;
- unsigned char *buffer;
- int filesize;
- int numslices;
- int grdriver, grmode, grerror;
- int tw;
- char title[20];
- char disp[128][128];
-
-
- /* Ensure proper syntax */
-
- if(argc != 2)
- {
- printf("Usage:: Display4 datafile.\n\n");
- exit(1);
- }
-
-
- /* open input files */
-
- datafile = fopen(argv[1], "rb");
- if(datafile == NULL)
- {
- printf("Can not open file: %s.\n\n", argv[1]);
- exit(2);
- }
-
-
- /* Create the LUT structure */
-
- for(i=0; i<64; i++) /* ramp window fxn */
- for(j=0; j<3; j++)
- {
- LUT[i][j] = (unsigned char)i;
- }
-
- for(i=64; i<128; i++) /* quadratic window fxn */
- for(j=0; j<3; j++)
- {
- temp = (float)(i-64)/63.0 * 7.9;
- LUT[i][j] = (unsigned char)temp*temp;
- }
-
- for(i=128; i<192; i++) /* sqrt window fxn */
- for(j=0; j<3; j++)
- {
- temp = (float)(i-128)/63.0 * 3968.0;
- LUT[i][j] = (unsigned char)sqrt((double)temp);
- }
-
- for(i=192; i<256; i++) /* inverse ramp window fxn */
- for(j=0; j<3; j++)
- {
- LUT[i][j] = (unsigned char)(63 - (i-192));
- }
-
-
- /* Determine the number of slices in the volume */
-
- filesize = filelength( fileno(datafile) );
- numslices = filesize / 256 / 256;
-
-
- /* Allocate memory for the disk buffer. */
-
- buffer = (unsigned char *)malloc( 256*256 );
- if(buffer == NULL)
- {
- printf("Error allocating memory for buffer.\n\n");
- exit(3);
- }
-
-
- /* check graphics hardware */
-
- grdriver = DETECTX;
- grmode = 0;
- detectgraph(&grdriver, &grmode);
- switch(grdriver)
- {
- case VESA256:
- case ATI256:
- case COMPAQ:
- case TSENG3256:
- case TSENG4256:
- case GENOA5:
- case GENOA6:
- case OAK:
- case PARADIS256:
- case TECMAR:
- case TRIDENT256:
- case VIDEO7:
- case VIDEO7II:
- break;
-
- default:
- printf("Unable to detect 256 color graphics adapter.\n");
- exit(4);
- break;
- }
-
-
- grmode = 1;
- initgraph(&grdriver, &grmode, "");
- grerror = graphresult();
- if(grerror)
- {
- closegraph();
- printf("Error %d initializing graphics mode.\n", grerror);
- exit(5);
- }
-
-
- /* Program the hardware palette */
-
- for(i=0; i<256; i++)
- setrgbpalette(i, LUT[i][0], LUT[i][1], LUT[i][2]);
-
-
- /* Display the image titles */
-
- setcolor(63);
-
- strcpy(title, "RAMP FUNCTION");
- tw = textwidth(title);
- outtextxy(160 - tw/2, 160, title);
-
- strcpy(title, "QUADRATIC FUNCTION");
- tw = textwidth(title);
- outtextxy(480 - tw/2, 160, title);
-
- strcpy(title, "SQUARE ROOT FUNCTION");
- tw = textwidth(title);
- outtextxy(160 - tw/2, 360, title);
-
- strcpy(title, "INVERSE RAMP FUNCTION");
- tw = textwidth(title);
- outtextxy(480 - tw/2, 360, title);
-
-
- while(!kbhit())
- {
- for(i=0; i<numslices; i++)
- {
-
- fseek(datafile, (long)i*256*256, SEEK_SET);
- fread(buffer, 256*256, 1, datafile);
-
- for(j=0; j<128; j++)
- for(k=0; k<128; k++)
- {
- l = (int)buffer[2*j*256 + 2*k];
- l += (int)buffer[(2*j+1)*256 + 2*k];
- l += (int)buffer[2*j*256 + 2*k+1];
- l += (int)buffer[(2*j+1)*256 + 2*k+1];
- l /= 16;
-
- disp[k][j] = l;
- }
-
- for(j=0; j<128; j++)
- for(k=0; k<128; k++)
- putpixel(96+k, 20+j, disp[k][j]);
-
- for(j=0; j<128; j++)
- for(k=0; k<128; k++)
- putpixel(416+k, 20+j, disp[k][j] | 0x40);
-
- for(j=0; j<128; j++)
- for(k=0; k<128; k++)
- putpixel(96+k, 220+j, disp[k][j] | 0x80);
-
- for(j=0; j<128; j++)
- for(k=0; k<128; k++)
- putpixel(416+k, 220+j, disp[k][j] | 0xC0);
-
- }
- }
-
- fclose(datafile);
- free( (void *)buffer);
- getch();
- closegraph();
- }
-